package com.admin.controller;

import cn.hutool.core.io.FileUtil;
import com.admin.entity.Admin;
import com.admin.entity.Station;
import com.admin.entity.TGroup;
import com.admin.entity.Theme;
import com.admin.service.IAdminService;
import com.admin.service.IStationService;
import com.admin.service.IThemeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/theme")
public class ThemeController {

    @Autowired
    private IThemeService themeService;

    @Autowired
    private IStationService stationService;

    @Autowired
    private IAdminService adminService;

    @Value("${upload.tmp.dir}")
    private String tmp;

    @Autowired
    HttpServletRequest request;

    /**
     * 查询所有主题
     *
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @RequestMapping(value = "query/themeAll", method = RequestMethod.GET)
    public ResponseBean queryThemeAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map map) {

        IPage themeIPage = themeService.queryThemeAll(page, limit, map);
        return new ResponseBean(0, themeIPage.getTotal(), themeIPage.getRecords());
    }

    /**
     * 查询管理所有主题
     *
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @RequestMapping(value = "query/guanli/themeAll", method = RequestMethod.GET)
    public ResponseBean queryGuanLiThemeAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map map) {

        IPage themeIPage = themeService.queryGuanLiThemeAll(page, limit, map);
        return new ResponseBean(0, themeIPage.getTotal(), themeIPage.getRecords());
    }

    /**
     * 根据用户查出来站点ID ， 在通过ID查
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @RequestMapping(value = "/query/themeUsernameALL",method = RequestMethod.GET)
    public ResponseBean queryThemeUsernameALL(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map map){

        String  username = (String) map.get("username");


        return null;
    }

    /**
     * 新增主题
     *
     * @param theme
     * @return
     */
    @RequestMapping(value = "add/theme", method = RequestMethod.POST)
    public ResponseBean addTheme(@RequestBody Theme theme) {
        int i=0;

        ResponseBean responseBean = ifThemeAdd(theme);

        if (responseBean.getCode() == 500) {
            return responseBean;
        }

        if(responseBean.getCode()==100){
            theme.setThemeAdmin(1);
            i = themeService.addTheme(theme);
        }else{
            i = themeService.addTheme(theme);
        }

        if (i > 0) {

            String path = request.getServletContext().getRealPath("images");

            File file = new File(path);

            if (file.exists() == false) {
                file.mkdirs();
            }
            FileUtil.copy(tmp + "/" + theme.getThemeImage(), path + "/" + theme.getThemeImage(), true);
            return new ResponseBean(200, "操作成功", "");
        } else {

            return new ResponseBean(500, "名字已被占用", "");
        }
    }

    /**
     * 管理员新增主题
     *
     * @param theme
     * @return
     */
    @RequestMapping(value = "add/themeAdmin", method = RequestMethod.POST)
    public ResponseBean addGuanLiTheme(@RequestBody Theme theme) {

        ResponseBean responseBean = ifThemeAddAdmin(theme);
        if (responseBean != null ) {
            return responseBean;
        }

        theme.setThemeAdmin(1);
        int i = themeService.addTheme(theme);

        if (i > 0) {

            String path = request.getServletContext().getRealPath("images");

            File file = new File(path);

            if (file.exists() == false) {
                file.mkdirs();
            }
            FileUtil.copy(tmp + "/" + theme.getThemeImage(), path + "/" + theme.getThemeImage(), true);
            return new ResponseBean(200, "操作成功", "");
        } else {

            return new ResponseBean(500, "名字已被占用", "");
        }
    }

    /**
     * 新增内置主题 的管理员认证
     *
     * @param theme
     * @return
     */
    @RequestMapping(value = "add/guanli/theme", method = RequestMethod.POST)
    public ResponseBean addGuanliTheme(@RequestBody Theme theme) {

        String token = request.getHeader("token");

        //查看是否是登录的用户操作
        int i = adminService.selectQuanxian(theme.getThemeId(), token);

        //查看返回的parent是否是 0
        Admin admin = adminService.queryAdminParent(theme.getThemeId());

        if(i==200 && admin.getAdminParent()==0){
            return new ResponseBean(200,"操作成功","");
        }

        return new ResponseBean(500,"操作失败","");
//
//        int i=0;
//
//        ResponseBean responseBean = ifThemeAdd(theme);
//        if (responseBean != null && responseBean.getCode() != 200) {
//            return responseBean;
//        }
//        if(responseBean.getCode()==200){
//
//             i = themeService.addTheme(theme);
//        }else{
//            theme.setThemeAdmin(1);
//            i = themeService.addTheme(theme);
//        }
//
//        if (i > 0) {
//            String path = request.getServletContext().getRealPath("images");
//
//            File file = new File(path);
//
//            if (file.exists() == false) {
//                file.mkdirs();
//            }
//            FileUtil.copy(tmp + "/" + theme.getThemeImage(), path + "/" + theme.getThemeImage(), true);
//            return new ResponseBean(200, "操作成功", "");
//        } else {
//            return new ResponseBean(500, "名字已被占用", "");
//        }
    }

    /**
     * 修改主题
     *
     * @param theme
     * @return
     */
    @RequestMapping(value = "/update/theme", method = RequestMethod.PUT)
    public ResponseBean updateTheme(@RequestBody Theme theme) {

        ResponseBean responseBean = ifTheme(theme);

        if (responseBean != null) {
            return responseBean;
        }

        int i = themeService.updateTheme(theme);

        if (i > 0) {
            String path = request.getServletContext().getRealPath("images");

            File file = new File(path);

            if (file.exists() == false) {
                file.mkdirs();
            }
            FileUtil.copy(tmp + "/" + theme.getThemeImage(), path + "/" + theme.getThemeImage(), true);
            return new ResponseBean(200, "操作成功", "");
        }

        return new ResponseBean(500, "操作失败", "");
    }


//    /**
//     * 每次保存，或者修改，都要变成，未审核状态 ,管理员例外
//     *
//     * @return
//     */
//    @RequestMapping(value = "/update/themeAudited", method = RequestMethod.PUT)
//    public ResponseBean updateThemeAudited(@RequestBody Theme theme) {
//
//        Theme themeStation = themeService.queryThemeStation(theme.getThemeId());
//
//        Map map = stationService.queryThemeAdmin(themeStation.getThemeStation());
//
//        if (map != null && map.size() > 0) {
//            return new ResponseBean(200, "操作成功", "");
//        }
//
//        themeStation.setThemeAuditPcAudited("");
//        themeStation.setThemeAuditAndroidAudited("");
//
//        boolean b = themeService.updateById(themeStation);
//
//        if (b) {
//            return new ResponseBean(200, "操作成功", "");
//        }
//
//        return new ResponseBean(500, "操作失败", "");
//    }

    /**
     * 查询未审核的
     * @param themeId
     * @return
     */
    @RequestMapping(value = "/query/unaudited",method = RequestMethod.GET)
    public String queryThemeUnaudited(@RequestParam("themeId") String themeId){

        Theme byId = themeService.getById(themeId);

        if(byId==null){
            return "无数据";
        }else{
            return byId.getThemeAuditPcUnaudited();
        }

    }

    /**
     * 查询已生效的
     * @param themeId
     * @return
     */
    @RequestMapping(value = "/query/audit",method = RequestMethod.GET)
    public String queryThemeAudit(@RequestParam("themeId") String themeId){

        Theme byId = themeService.getById(themeId);

        if(byId==null){
            return "无数据";
        }else{
            return byId.getThemeAuditPcAudited();
        }

    }

    public ResponseBean ifThemeAdd(Theme theme) {

        if (theme.getThemeName() == null || theme.getThemeName() == "") {
            return new ResponseBean(500, "必填参数未填写", "");
        }

        theme.setThemeAuditPcUnaudited("0");
        theme.setThemeAuditAndroidUnaudited("0");

        Map list = stationService.queryThemeAdmin(theme.getThemeStation());

        if (list != null && list.size() == 0) {
            return new ResponseBean(200, "不是管理员", "");
        }

        theme.setThemeAuditAndroidAudited("0");
        theme.setThemeAuditPcAudited("0");

        System.out.println("shisi");
        return new ResponseBean(100, "添加失败", "");
    }

    public ResponseBean ifThemeAddAdmin(Theme theme) {

        if (theme.getThemeName() == null || theme.getThemeName() == "") {
            return new ResponseBean(500, "必填参数未填写", "");
        }

        theme.setThemeAuditPcUnaudited("0");
        theme.setThemeAuditAndroidUnaudited("0");
        theme.setThemeAuditAndroidAudited("0");
        theme.setThemeAuditPcAudited("0");

        return null;
    }

    /**
     * 自增分组参数的判断,和以防有人非法乱改数据
     *
     * @param
     * @return
     */
    public ResponseBean ifTheme(Theme theme) {

        if (theme.getThemeId() == null) {
            return new ResponseBean(500, "操作失败", "未获取到主题ID");
        }

        if (theme.getThemeName() != null) {

            List<Theme> list = themeService.list(null);

            for (Theme group : list) {

                if (theme.getThemeId() != group.getThemeId() && theme.getThemeName().equals(group.getThemeName())) {
                    return new ResponseBean(500, "操作失败", "主题名称已被占用");
                }
            }
        }

        return null;
    }

    /**
     * 查询未审核的主题
     *
     * @return
     */
    @RequestMapping("/selectPcuncheckedTheam")
    public ResponseBean selectPcuncheckedTheam(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        IPage p = themeService.selectPcuncheckedTheam(page, limit);
        return new ResponseBean(0, p.getTotal(), p.getRecords());
    }

    /**
     *
     * @return
     */
   @RequestMapping("/updatePcchecked")
    public ResponseBean updatePcchecked(Theme theme){
        theme.setThemeAuditPcAudited("已审核");
        boolean f=themeService.updateById(theme);
        int code=f?200:500;
        return new ResponseBean(code,0,null);
    }

    /**
     * 酒店/足浴欢迎界面审核
     */
    @RequestMapping("/welcomepageshenhe")
    public ResponseBean  welcomepageshenhe_jiudian(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit){
        IPage p=themeService.selwelcomepageshenge(page,limit);
        return new ResponseBean(0,p.getTotal(),p.getRecords());

    }
    /**
     * 修改审核状态
     */
    @RequestMapping("/updateshenhe")
    public ResponseBean updeshenhe(Theme s) {
        s.setThemeWelcomePagePcchecked("已审核");
        boolean f = themeService.updateById(s);
        int code = f ? 200 : 500;
        return new ResponseBean(code, null, null);
    }

    /**
     * 查找审核管理里面里面所有未审核的数量返回一个map
     * @return
     */
    @RequestMapping("/selWeishenheCount")
    public ResponseBean selWeishenheCount(){
        Map map=themeService.welcomeWeichenheCount();
        return new ResponseBean(0,0,map);
    }
}
